package com._119yn.host.gst200.database.dao.impl;

import com._119yn.host.gst200.database.dao.HostSessionDao;
import com._119yn.host.gst200.database.entity.HostSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class HostSessionDaoImpl implements HostSessionDao {

    private final JdbcTemplate jdbcTemplate;

    @Override
    public long insert(HostSession hostSession) {
        String sql = "insert into HOST_SESSION (DEVICE_ID, DEVICE_ADDRESS, DEVICE_TYPE, HOST_ID, OFFLINE, SILENCE, RES_STA, MANUAL_STA, AUTO_STA, SPRAY_STA, GUARD_STA, SEQUENCE, STATUS, CREATE_TIME, LAUNCH_TIME, ACTIVE_TIME) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(
                sql,
                hostSession.getDeviceId(),
                hostSession.getDeviceAddress(),
                hostSession.getDeviceType(),
                hostSession.getHostId(),
                hostSession.getOffline(),
                hostSession.getSilence(),
                hostSession.getResSta(),
                hostSession.getManualSta(),
                hostSession.getAutoSta(),
                hostSession.getSpraySta(),
                hostSession.getGuardSta(),
                hostSession.getSequence(),
                hostSession.getStatus(),
                hostSession.getCreateTime(),
                hostSession.getLaunchTime(),
                hostSession.getActiveTime()
        );
    }

    @Override
    public long deleteByHostId(int hostId) {
        String sql = "DELETE FROM HOST_SESSION WHERE HOST_ID = ?";
        return jdbcTemplate.update(sql, hostId);
    }

    @Override
    public long updateByHostId(HostSession hostSession) {
        String sql = "UPDATE HOST_SESSION SET DEVICE_ID = ?, DEVICE_ADDRESS = ?, DEVICE_TYPE = ?, OFFLINE = ?, SILENCE = ?, RES_STA = ?, MANUAL_STA = ?, AUTO_STA = ?, SPRAY_STA = ?, GUARD_STA = ?, SEQUENCE = ?, STATUS = ?, CREATE_TIME = ?, LAUNCH_TIME = ?, ACTIVE_TIME = ? WHERE HOST_ID = ?";
        return jdbcTemplate.update(
                sql,
                hostSession.getDeviceId(),
                hostSession.getDeviceAddress(),
                hostSession.getDeviceType(),
                hostSession.getOffline(),
                hostSession.getSilence(),
                hostSession.getResSta(),
                hostSession.getManualSta(),
                hostSession.getAutoSta(),
                hostSession.getSpraySta(),
                hostSession.getGuardSta(),
                hostSession.getSequence(),
                hostSession.getStatus(),
                hostSession.getCreateTime(),
                hostSession.getLaunchTime(),
                hostSession.getActiveTime(),
                hostSession.getHostId()
        );
    }

    @Override
    public HostSession selectByHostId(int hostId) {
        String sql = "SELECT * FROM HOST_SESSION WHERE HOST_ID = ?";
        List<HostSession> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HostSession.class), hostId);
        if (list.isEmpty()) {
            return null;
        }
        return list.getFirst();
    }

    @Override
    public List<HostSession> selectAll() {
        String sql = "SELECT * FROM HOST_SESSION";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HostSession.class));
    }

    @Override
    public long truncate() {
        String sql = "TRUNCATE TABLE HOST_SESSION";
        return jdbcTemplate.update(sql);
    }

    @Override
    public long updateSilenceAndActiveTime(Integer silence, LocalDateTime activeTime) {
        String sql = "UPDATE HOST_SESSION SET SILENCE = ?, ACTIVE_TIME = ? WHERE OFFLINE = 0";
        return jdbcTemplate.update(sql, silence, activeTime);
    }

    @Override
    public long updateStatusesAndActiveTime(Long status, Integer manualSta, Integer autoSta, Integer spraySta, Integer guardSta, LocalDateTime activeTime) {
        String sql = "UPDATE HOST_SESSION SET STATUS = ?, MANUAL_STA = ?, AUTO_STA = ?, SPRAY_STA = ?, GUARD_STA = ?, ACTIVE_TIME = ? WHERE OFFLINE = 0";
        return jdbcTemplate.update(sql, status, manualSta, autoSta, spraySta, guardSta, activeTime);
    }

    @Override
    public List<HostSession> selectByOffline(Integer offline) {
        String sql = "SELECT * FROM HOST_SESSION WHERE OFFLINE = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HostSession.class), offline);
    }

    @Override
    public boolean isAllHostSessionOffline() {
        String sql = "SELECT SUM(1) = SUM(OFFLINE) FROM HOST_SESSION;";
        return Boolean.TRUE.equals(jdbcTemplate.queryForObject(sql, Boolean.class));
    }

}
